<?php
/**
 * desc Created by wang.
 * User: wang
 * motto: 是时候为小时候吹过的牛而努力了!
 * Date: 2020/3/27 21:32
 */

namespace app\admin\controller;

use think\facade\Filesystem;
use think\facade\Config;

class Image extends AdminBase
{
    /**
     * 功能说明 头像上传
     * @author: wang
     * @Date: 2020/3/30 10:06
     * @return \think\response\Json
     */
    public function icon()
    {
        if (!$this->request->isPost()) {
            return $this->show(config('status.error', '请求不合法'));
        }
        $file = request()->file('file');
        $param = Config::load('upload/images', 'images');
//        $fileinfo = request()->file();
//        try {
//            validate(['image' => 'filesize:10|fileExt:jpg'])->check($fileinfo);
//        } catch (think\exception\ValidateException $e) {
//            return $this->show(config('status.error'), $e->getMessage());
//        }

        if ($param['icon']['is_thumb'] == 1) {
            $object_image = \think\Image::open($file->getPathName());
            $object_image->thumb($param['icon']['max_width'], $param['icon']['max_height']);
            $object_image->save($file->getPathName());
        }
        $filename = Filesystem::disk('public')->putFile('upload/image/amin_icon', $file);
        if (!$filename) {
            return $this->show(config('status.error'), '上传icon失败');
        }
        $imgUrl = [
            'imgUrl' => $filename
        ];
        return $this->show(config('status.success'), '上传成功', $imgUrl);
    }
}